package com.njlime.cms.service.sys;

import com.njlime.cms.entities.Information;
import com.njlime.cms.repository.sys.InformationDao;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.sys.entities.Unit;
import com.njlime.sys.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 热点相关Service
 * Created by Dean on 2017-8-9.
 */
@Service
@Transactional(readOnly = true)
public class InformationService {

    @Resource
    private InformationDao informationDao;

    /**
     * 获取单个实体
     *
     * @param id
     * @return
     */
    public Information findOne(Integer id) {
        return informationDao.findOne(id);
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public Map<String,Object> get(Integer id) {
        return informationDao.get(id);
    }

    /**
     * 保存
     *
     * @param information
     * @return
     */
    @Transactional(readOnly = false)
    public Integer save(Information information) {
        if (information.getPublish_status() != null && information.getPublish_status() != GlobalValue.PublishStatus.UNPUBLISHED) {//不是定时发送的，则发送时间为当前时间
            information.setPublish_time(new Date());
        }
        information.setComment_num(0);
        information.setReading(0);
        information.setIs_del(GlobalValue.NO);
        information.setCreate_time(new Date());
        information.setIs_comment(GlobalValue.NO);
        informationDao.save(information);
        return information.getId();
    }

    /**
     * 编辑
     *
     * @param information
     * @return
     */
    @Transactional(readOnly = false)
    public int editInfo(Information information) {
        return informationDao.editInfo(information);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int del(int id) {
        return informationDao.del(id);
    }

    /**
     * 评论开关
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int toggleComment(int id) {
        return informationDao.toggleComment(id);
    }

    /**
     * 上下线
     *
     * @param information
     * @return
     */
    @Transactional(readOnly = false)
    public int togglePublish(Information information) {
        if(information.getPublish_status() != GlobalValue.PublishStatus.PUBLISHED){
            information.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        }else{
            information.setPublish_status(GlobalValue.PublishStatus.CANCELED);
        }
        return informationDao.editInfo(information);
    }

    /**
     * 获取列表
     *
     * @param pageBean
     * @param information
     * @return
     */
    public PageBean list(PageBean pageBean, Information information, boolean is_all) {
        return informationDao.list(pageBean, information, is_all);
    }

    /**
     * 更新评论数
     * @param id
     */
    @Transactional(readOnly = false)
    public void updateCommentNum(Integer id, Integer num){
        informationDao.updateCommentNum(id, num);
    }
    /**
     * 根据频道和栏目查询列表
     * @param channelId 频道id
     * @param columnId 栏目id 0表示无栏目
     * @param isBannel 是否显示轮播 0表示不显示
     *
     * @return
     */
    public List listByChannelId(int channelId, int columnId,int isBannel) {
        return informationDao.listByChannelId(channelId, columnId,isBannel);
    }

    /**
     * 欢迎页面统计
     * @param unit
     * @return
     */
    public Map<String,Object> welcomeReport(Unit unit){
        return informationDao.welcomeReport(unit);
    }
}
